/**
 *
 */
package com.naiterui.ehp.bs.emr.controller.patient;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestObject;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IPatientCaseService;
import com.naiterui.ehp.bs.emr.utils.Constants;
import com.naiterui.ehp.bs.emr.vo.patient.CaseFileResultVO;
import com.naiterui.ehp.bs.emr.vo.patient.CaseFileVO;
import com.naiterui.ehp.bs.emr.vo.patient.DocumentInitVO;
import com.naiterui.ehp.bs.emr.vo.patient.DocumentResultVO;
import com.naiterui.ehp.bs.emr.vo.patient.DocumentVO;
import com.naiterui.ehp.bs.emr.vo.patient.PatientCaseDetailVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 患者病例controller
 *
 * @since 2.16
 */
@Validated
@RestController
@RequestMapping("emr/patient/case")
@Api(tags = SwaggerTag.PATIENT + SwaggerTag.MEDICAL_RECORD)
public class EmrPatientCaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmrPatientCaseController.class);

    @Autowired
    private IPatientCaseService patientCaseService;

    @ApiOperation(value = "初始化基本病情信息", notes = "初始化基本病情信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "familyId", value = "家庭成员id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "doctorId", value = "关联医生id", required = false, dataType = "long", paramType = "query")})
    @PostMapping("init")
    public ResponseVO<DocumentInitVO> initCase(@NotNull(message = "患者id不能为null") Long patientId,
        @NotNull(message = "家庭成员id不能为null") Long familyId, Long doctorId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("初始化基本病情信息 请求参数：patientId = {}, familyId = {}, doctorId = {}", patientId, familyId, doctorId);
        DocumentInitVO documentInitVO = patientCaseService.initDocument(patientId, familyId);
        return PublicService.returnResponseVO(documentInitVO);
    }

    @ApiOperation(value = "保存基本病情信息", notes = "保存基本病情信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "familyId", value = "家庭成员id", required = true, dataType = "long", paramType = "query")})
    @PostMapping("saveDocument")
    public ResponseVO<DocumentResultVO> saveDocument(@NotNull(message = "患者id不能为null") Long patientId,
        @NotNull(message = "家庭成员id不能为null") Long familyId,
        @RequestObject @Valid DocumentVO paramVO) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("保存基本病情信息 请求参数：patientId = {}, familyId = {}, paramVO = {}", patientId, familyId, paramVO);
        String description = paramVO.getDescription();
        if (StringUtils.isNotEmpty(description)) {
            int length = description.length();
            if (length < 3 || length > 1000) {
                LOGGER.warn("病情描述长度不符合规范:{}", description);
                throw new BusinessException(ExceptionCodes.INPUT_TOO_LONG);
            }
        }
        DocumentResultVO resultVO = patientCaseService.saveDocument(patientId, familyId, paramVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, resultVO);
    }

    @ApiOperation(value = "保存病例信息", notes = "保存病例信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "familyId", value = "家庭成员id", required = true, dataType = "long", paramType = "query")})
    @PostMapping("save")
    public ResponseVO<CaseFileResultVO> saveCase(@NotNull(message = "患者id不能为null") Long patientId,
        @NotNull(message = "家庭成员id不能为null") Long familyId,
        @RequestObject @NotNull(message = "paramVO 不能为null") CaseFileVO paramVO) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("保存病例信息 请求参数：patientId = {}, familyId = {}, paramVO = {}", patientId, familyId, paramVO);
        CaseFileResultVO caseFileResultVO = patientCaseService.saveCaseFile(patientId, familyId, paramVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, caseFileResultVO);
    }

    @ApiOperation(value = "发送病例信息", notes = "发送病例信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "caseId", value = "病例id", required = true, dataType = "string", paramType = "query")})
    @PostMapping("send")
    public ResponseVO<CaseFileResultVO> sendCase(@NotNull(message = "患者id不能为null") Long patientId,
        @NotNull(message = "医生id不能为null") Long doctorId,
        @NotEmpty(message = "病例id不能为null") String caseId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("发送病例信息 请求参数：patientId = {}, doctorId = {}, caseId = {}", patientId, doctorId, caseId);
        Integer needPay = patientCaseService.sendCase(patientId, doctorId, caseId);
        boolean needPayFlag = false;
        if (needPay == Constants.IM_RET_NEEDPAY) {
            needPayFlag = true;
        }
        CaseFileResultVO caseFileResultVO = new CaseFileResultVO(needPayFlag, doctorId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, caseFileResultVO);
    }


    @ApiOperation(value = "获取指定医生填写发送至患者的病历列表", notes = "获取医生填写病例列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query")})
    @PostMapping("listByDoctor")
    public ResponseVO<PageVO<PatientCaseDetailVO>> listByDoctor(@NotNull(message = "患者ID不能为null") Long patientId,
        @NotNull(message = "医生ID不能为null") Long doctorId, PageParamVO pageVO) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("获取医生填写病例列表 请求参数：patientId = {}, doctorId = {}, pageVO = {}", patientId, doctorId, pageVO);
        PageVO<PatientCaseDetailVO> patientCasePageVO = patientCaseService.getCasePageListByDoctor(patientId, doctorId, pageVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, patientCasePageVO);
    }


    //========================海南分割线========================================

    @ApiOperation(value = "获取病例详情", notes = "获取病例详情", tags = SwaggerTags.WX_PATIENT_INQUIRER)
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "caseId", value = "病例id", required = true, dataType = "string", paramType = "query")})
    @PostMapping("detail")
    public ResponseVO<PatientCaseDetailVO> detail(Long patientId,
        @NotEmpty(message = "病例id不能为null") String caseId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("获取病例详情 请求参数：patientId = {}, caseId = {}", patientId, caseId);
        PatientCaseDetailVO caseFileResultVO = patientCaseService.getCaseDetail(patientId, caseId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, caseFileResultVO);
    }

    @ApiOperation(value = "获取病例列表", notes = "获取病例列表", tags = SwaggerTags.WX_PATIENT_INQUIRER)
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "inquirerId", value = "就诊人id", required = true, dataType = "long", paramType = "query")})
    @PostMapping("list")
    public ResponseVO<PageVO<PatientCaseDetailVO>> list(@NotNull(message = "患者ID不能为null") Long patientId,
        @NotNull(message = "就诊人id不能为空") Long inquirerId,
        PageParamVO pageVO) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("获取病例列表 请求参数：patientId = {}, inquirerId={}, pageVO = {}", patientId, inquirerId, pageVO);
        PageVO<PatientCaseDetailVO> patientCasePageVO = patientCaseService.getCasePageList(patientId, inquirerId, pageVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, patientCasePageVO);
    }


}
